package com.ll.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;


/**
 * ListVals校验器  实现ConstraintValidator<自定义注解,参数类型>
 *
 * @author 21243
 */
public class ListValsConstraintValidator implements ConstraintValidator<ListVals, Integer> {

    Set<Integer> params = new HashSet<>();

    @Override
    public void initialize(ListVals constraintAnnotation) {
        //初始化 拿到参数

        int[] vals = constraintAnnotation.vals();
        if (vals != null && vals.length > 0) {
            for (int val : vals) {
                params.add(val);
            }
        }

    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        //包含参数指定值返回true
        return params.contains(value);
    }

}
